#include <string.h>

#include "lib.h"

#include "rlround.h"
#include "rlbet.h"

RouletteRound::RouletteRound(void)
{
	m_numBets = 0;
	m_winner = (uint8)-1;

	memset(m_bets, 0, sizeof(RouletteBet*) * MAX_BETS);
}

RouletteRound::~RouletteRound(void)
{
	int32 i;

	for (i = 0; i < MAX_BETS; i++)
		delete m_bets[i];
}

uint8 
RouletteRound::numMoneyBets()
{
	int32 i;
	uint8 numBets = 0;
	RouletteBet *bet;

	for (i = 0; i < MAX_BETS; i++)
	{
		bet = m_bets[i];
		if (bet && bet->getAmount() > 0)
			numBets++;
	}

	return numBets;
}

int32
RouletteRound::totalBet(void)
{
	int32 i;
	int32 total = 0;
	RouletteBet *bet;

	for (i = 0; i < MAX_BETS; i++)
	{
		bet = m_bets[i];
		if (bet)
			total += bet->getAmount();
	}

	return total;
}

int32
RouletteRound::totalInside(void)
{
	int32 i;
	int32 total = 0;
	RouletteBet *bet;

	for (i = 0; i < MAX_BETS; i++)
	{
		bet = m_bets[i];
		if (bet && bet->isInside())
			total += bet->getAmount();
	}

	return total;
}

int32
RouletteRound::totalOutside(void)
{
	int32 i;
	int32 total = 0;
	RouletteBet *bet;

	for (i = 0; i < MAX_BETS; i++)
	{
		bet = m_bets[i];;
		if (bet && bet->isOutside())
			total += bet->getAmount();
	}

	return total;
}

bool8 
RouletteRound::addBet(RouletteBet *bet)
{
	if (bet && m_numBets < MAX_BETS && !m_bets[bet->getBetID()])
	{
		m_bets[bet->getBetID()] = bet;
		m_numBets++;
		return true;
	}
	else
		return false;
}

int32
RouletteRound::calcPayout(uint8 stop) 
{
	int32 i, payout;
	RouletteBet* bet;

	// Payout includes original bet
	//
	payout = 0;

	for (i = 0; i < MAX_BETS; i++)
	{
		bet = getBet((uint8)i);
		if (bet && bet->hasNumber(stop))
			payout += (bet->getOdds() * bet->getAmount() + bet->getAmount());
	}

	return payout;
}

int32
RouletteRound::calcTotalWinningBets(uint8 stop)
{
	int32 i, total;
	RouletteBet* bet;

	total = 0;

	for (i = 0; i < MAX_BETS; i++)
	{
		bet = getBet((uint8)i);
		if (bet && bet->hasNumber(stop))
			total += bet->getAmount();
	}

	return total;
}

bool8
RouletteRound::validate(int32 minIn, int32 maxIn, int32 minOut, int32 maxOut, int32 maxSingleIn)
{
	int32 totalin = totalInside();
	int32 totalout = totalOutside();

	// Must have at least 1 bet
	//
	if (numBets() <= 0)
		return false;

	// Must have bet total > 0
	//
	if (totalBet() <= 0)
		return false;

	// check maxSingleIn
	if (totalin)
	{
		for (int32 i = 0; i < MAX_BETS; i++)
		{
			RouletteBet* bet = m_bets[i];
			if (bet && bet->isInside())
			{
				if (bet->numNumbers() > 0 &&
					bet->numNumbers() <= 6)
				{
					if (bet->getAmount() > (maxSingleIn * bet->numNumbers() ) )
						return false;
				}
				else
					return false;
			}
		}
	}

	// check the rest
	if (totalin && totalout)
	{
		if (totalin < minIn || totalin > maxIn ||
			totalout < minOut || totalout > maxOut)
			return false;
	}
	else if (totalin)
	{
		if (totalin < minIn || totalin > maxIn)
			return false;
	}
	else if (totalout)
	{
		if (totalout < minOut || totalout > maxOut)
			return false;
	}

    // RPF20090909 CGC No negative or cero bets (CSE-387)
      if (!isValidAmountBet())
            return false;

	return true;
}

bool8
RouletteRound::isValidAmountBet()
{
      int32 i;
      RouletteBet* bet;
      for (i = 0; i < MAX_BETS; i++)
      {
            bet = getBet((uint8)i);
            if (bet && bet->getAmount()<=0)
             return false;
      }
      return true;
}


